package com.pcbly.web.action.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.system.MenuBean;
import com.pcbly.web.bean.system.MenuChildBean;
import com.pcbly.web.business.menu.MenuService;

public class MenuAction extends BaseAction<MenuBean> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6618833274885289262L;
	protected MenuBean menu;
	protected List<MenuBean> menus;
	private String id;
	private String ids;
	protected MenuService menuService;
	protected List<MenuChildBean> menuChildBeans;

	private String parentId;

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public List<MenuChildBean> getMenuChildBeans() {
		return menuChildBeans;
	}

	public void setMenuChildBeans(List<MenuChildBean> menuChildBeans) {
		this.menuChildBeans = menuChildBeans;
	}

	public String save() throws Exception {
		menuService.saveMenu(menu);
		@SuppressWarnings("unused")
		boolean isNew = (menu.getId() == null || "0".equals(menu.getId()));

		return SUCCESS;
	}

	public String index() throws Exception {
		menuChildBeans = menuService.queryMenuChildBeansByParentId(null);
		return super.index();
	}

	public String dwrDelete(String id) throws Exception {
//		try{
			if(log.isDebugEnabled()){
				log.debug("login dwrDelete.....");
				log.debug(id);
			}
			if (id == null || "".equals(id.trim())) {
				return "delete error!";
			} else {
				menuService.removeMenu(id);
				return "delete ok!";
			}
//		}catch(Exception e){
//			e.printStackTrace();
//			return "delete error!";
//		}
	}

	public MenuBean dwrView(String id) throws Exception {
		if (id == null || "".equals(id.trim())) {
			return null;
		} else {
			menu=menuService.getMenuById(id);
			return menu;
		}
	}

	public String dwrSave(MenuBean bean) throws Exception {		
		if (bean.getId() == null || "".equals(bean.getId().trim())) {
			menuService.saveMenu(bean);
			return "save ok!";
		} else {
			menuService.saveMenu(bean);
			return "update ok!";
		}
	}

	public String child() throws Exception {
		menuChildBeans = menuService.queryMenuChildBeansByParentId(parentId);
		StringBuilder sb = new StringBuilder();
		HttpServletResponse response = getResponse();
		HttpServletRequest request = getRequest();
		String WEBPATH = request.getContextPath();
		sb.append("<?xml  version=\"1.0\" encoding=\"UTF-8\"?><root>");
		for (MenuChildBean bean : menuChildBeans) {
			sb.append("<tree text=\"");
			sb.append(htmEncode(bean.getName()));
			sb.append("\" ");
			if (bean.getChild() < 1) {
				sb.append(" action=\"javascript:showMenu('");
				sb.append(bean.getId());
				// sb.append("','");
				// sb.append(getId());
				sb.append("');\"  icon=\"");
				sb.append(WEBPATH);
				sb.append("/images/xloadtree/page_white_gear.gif\"/>");
			} else {
				sb.append(" action=\"javascript:showMenu('");
				sb.append(bean.getId());
				sb.append("');\"  src=\"");
				sb.append(WEBPATH);
				sb.append("/system/menu/child.crm");
				sb.append("?parentId=");
				sb.append(bean.getId());
				sb.append("\"/>");
			}
		}
		sb.append("</root>");
		response.setContentType("text/xml; charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
//		System.out.println(sb);
		try {
			response.getWriter().write(sb.toString());
			response.getWriter().flush();
			response.getWriter().close();
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String edit() throws Exception {

		return SUCCESS;
	}

	public String delete() throws Exception {
		if (!(id == null)) {
			menuService.removeMenu(id);
		}

		return SUCCESS;
	}

	public MenuBean getMenu() {
		return menu;
	}

	public void setMenu(MenuBean menu) {
		this.menu = menu;
	}

	public List<MenuBean> getMenus() {
		return menus;
	}

	public void setMenus(List<MenuBean> menus) {
		this.menus = menus;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
